package com.atjava.nebula.web.action;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atjava.nebula.core.dao.ibatis.Condition;
import com.atjava.nebula.core.exception.baseExp.BusinessException;
import com.atjava.nebula.core.exception.baseExp.SystemException;
import com.atjava.nebula.core.model.BaseModel;
import com.atjava.nebula.core.service.GenericService;
import com.atjava.nebula.core.util.entity.EntitySupport;
import com.atjava.nebula.web.view.XmlViewManager;

/**
 * The base update action of entity.
 * 
 * @author jin-dong
 *
 * @param <T>
 */
@Controller
@RequestMapping("/base/edit")
public class EditAction<T extends BaseModel,ID extends Serializable> extends BaseViewAction{
	
	/**
	 * The action to update page.
	 * 
	 * @param response
	 * @param catalog
	 * @param entity
	 * @param pkstr primary key string . eg: a=1 , b=2 , c=3
	 * @param modelMap
	 * @return entity
	 * @throws SystemException
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/view/**")
	@ResponseBody
	public T toAddUpdateView(HttpServletRequest request,
			@RequestParam(REQ_PARAM_QUERYPK) String pkstr,
			@RequestParam(REQ_PARAM_CATALOG) String catalog,
			@RequestParam(REQ_PARAM_ENTITY) String entity,
			ModelMap modelMap)  throws SystemException, BusinessException{
		
		GenericService<T,ID> baseService = (GenericService<T, ID>) getService(catalog,entity);
		
		T model = null;
		
		//update
		if(StringUtils.isNotBlank(pkstr)){
			
			Condition cond;
			cond = EntitySupport.getPkToCond(pkstr);
		
			List<T> entitys= baseService.list(cond);
			if(entitys.size() > 0){
				model = entitys.get(0);
			}
		}
		
		return model;
		
	}

	
	/**
	 * The action of do-update.
	 * 
	 * @param request
	 * @param response
	 * @param catalog
	 * @param entity
	 * @param modelMap
	 * @return result
	 * @throws SystemException
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/do/**",method = RequestMethod.POST)
	@ResponseBody
	public T doAddUpdate(HttpServletRequest request,
			@RequestParam(REQ_PARAM_QUERYPK) String pkstr,
			@RequestParam(REQ_PARAM_CATALOG) String catalog,
			@RequestParam(REQ_PARAM_ENTITY) String entity,
			ModelMap modelMap)  throws SystemException, BusinessException{
		
		GenericService<T,ID> baseService = (GenericService<T, ID>) getService(catalog,entity);
		Class<?> entityClass = XmlViewManager.getEntityClass(catalog, entity);
		T model = (T) getModelFromRequest(entityClass, request);
		
		//update
		if(StringUtils.isNotBlank(pkstr)){
			
			EntitySupport.setPkToEntity(entityClass, model ,pkstr);
			baseService.updateAllById(model);
			
		//add
		}else{
			
			baseService.create(model);
			
		}
		
		return model;
	}
	
}
